package es.titoapps.spanishhandball.iu;

import es.titoapps.spanishhandball.R;
import es.titoapps.spanishhandball.configuration.Configuration;
import es.titoapps.spanishhandball.database.DatabaseUtilities;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;

public class PresentationActivity extends Activity {
	
	
	// Constantes	
		protected int presentationTime = 2000;
		
		//Logica	
		private PresentationTask task;

	
	/**
	 * View Lifecycle
	 */

	public void onCreate(Bundle savedInstanceState) {
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.presentationlayout);
		
		task = new PresentationTask();
		task.execute();
		
		DatabaseUtilities.createDataBase(this);
        Configuration.loadConfiguration(this);
	}
	
	@Override
	protected void onDestroy() {
		task.cancel(true);
		super.onDestroy();
	}
	
	
	private class PresentationTask extends AsyncTask<Integer, Integer, Integer> {
		protected Integer doInBackground(Integer... input) {
			Thread t = new Thread() {
				@Override
				public void run() {
					try {
						sleep(presentationTime);
					} catch (InterruptedException e) {
					}
				}
			};
			try {
				t.start();
				t.join();
			} catch (InterruptedException e) {
			}
			return 0;
		}

		protected void onProgressUpdate(Integer... progress) {
		}

		protected void onPostExecute(Integer result) {	
			// Limpiamos la cache del navegador solo en pruebas
			startapp();
		}
	}

	/**
	 * Carga la pantalla de Menu
	 * @category Logica Inicio
	 */
	private void startapp() {
		Intent mainIntent = new Intent(PresentationActivity.this, HandballMainActivity.class);
		startActivity(mainIntent);
		finish();
	}
}
